package conson.tech.manage.facade.controller.coupons;


import cn.hutool.json.JSONUtil;
import conson.tech.core.constant.SecurityConstants;
import conson.tech.core.response.Response;
import conson.tech.manage.coupons.entity.dto.CouponsAuditDto;
import conson.tech.manage.coupons.entity.dto.CouponsAuditListDto;
import conson.tech.manage.coupons.entity.dto.CouponsDto;
import conson.tech.manage.coupons.entity.dto.CouponsSearchDto;
import conson.tech.manage.coupons.entity.vo.MerchantInformationVo;
import conson.tech.manage.coupons.service.CouponsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Api(value = "优惠券审核模块", tags = "优惠券审核模块")
@RestController
@RequestMapping((SecurityConstants.MANAGERAPI + "/coupons"))
public class CouponsController extends BaseController {


    @Autowired
    private CouponsService couponsService;


    @ApiOperation(value = "新增优惠券")
    @ApiImplicitParam(name = "dto", value = "新增优惠券参数", required = true, dataType = "CouponsDto")
    @PostMapping("/coupons/add")
    public Response addCoupons(HttpServletRequest request, @RequestBody CouponsDto dto) {

        log.info("优惠券新增入参:[{}]", JSONUtil.toJsonStr(dto));
        try {
            //获取用户id
            // long userId = this.getCurrentUserId(request);
            //获取用户名称
            // String userName = this.getCurrentUsername(request);
            Long userId = 1234567890L;
            String userName = "广电测试";
            return this.couponsService.addCoupons(dto, userId, userName);
        } catch (Exception e) {
            log.error("优惠券新增出现异常", e);
            return Response.fail("优惠券新增异常，请稍后重试");
        }
    }

    @ApiOperation(value = "优惠券作废")
    @ApiImplicitParam(name = "couponsId", value = "优惠券批次id", required = true, dataType = "Long")
    @PostMapping("/coupons/del/{couponsId}")
    public Response delCoupons(HttpServletRequest request, @ApiParam("优惠券id") @PathVariable("couponsId") Long couponsId) {

        log.info("优惠券作废入参:[{}]", couponsId);

        try {
//            //获取用户id
//            long userId =this.getCurrentUserId(request);
//            //获取用户名称
//            String userName = this.getCurrentUsername(request);

            Long userId = 123456789L;
            String userName = "广电测试";
            return this.couponsService.delCoupons(couponsId, userId, userName);
        } catch (Exception e) {
            log.error("优惠券作废失败", e);
            return Response.fail("优惠券作废失败，请稍后重试");
        }
    }


    @ApiOperation(value = "修改优惠券信息")
    @ApiImplicitParam(name = "dto", value = "优惠券修改内容", required = true, dataType = "CouponsDto")
    @PostMapping("/coupons/edit/{couponsId}")
    public Response editCoupons(HttpServletRequest request, @RequestBody CouponsDto dto, @ApiParam("优惠券id") @PathVariable("couponsId") Long couponsId) {

        log.info("优惠券修改入参:[{}]", JSONUtil.toJsonStr(dto));
        try {
            //获取用户id，用户名称
//            Long userId  =this.getCurrentUserId(request);
//            String userName = this.getCurrentUsername(request);
            Long userId = 123456789L;
            String userName = "广电测试";


            return this.couponsService.editCoupons(dto, userId, couponsId, userName);
        } catch (Exception e) {
            log.error("优惠券修改失败", e);
            return Response.fail("优惠券修改失败，请联系管理员处理");
        }
    }

    @ApiOperation(value = "查询优惠券批次列表")
    @ApiImplicitParam(name = "dto", value = "优惠券列表查询", required = true, dataType = "CouponsSearchDto")
    @PostMapping("/coupons/search-list/{pageSize}/{pageIndex}")
    public Response searchCouponsList(@ApiParam("每页条数") @PathVariable("pageSize") Integer pageSize,
                                      @ApiParam("页码") @PathVariable("pageIndex") Integer pageIndex,
                                      @ApiParam(value = "查询参数") @RequestBody CouponsSearchDto dto) {


        return Response.success(this.couponsService.searchCouponsList(pageSize, pageIndex, dto));

    }

    @ApiOperation(value = "查询优惠券详细信息")
    @ApiImplicitParam(name = "couponsId", value = "优惠券批次id", required = true, dataType = "Long")
    @GetMapping("/coupons/detail/{couponsId}")
    public Response detailCoupons(@ApiParam("优惠券id") @PathVariable("couponsId") Long couponsId) {

        return Response.success(this.couponsService.detailCoupons(couponsId));
    }

    @ApiOperation(value = "优惠券提交审核")
    @ApiImplicitParam(name = "couponsId", value = "优惠券批次id", required = true, dataType = "Long")
    @GetMapping("/coupons/audit/{couponsId}")
    public Response audtiCoupon(HttpServletRequest request, @ApiParam("优惠券id") @PathVariable("couponsId") Long couponsId) {


        //获取用户id
//        Long userId = this.getCurrentUserId(request);
//        String userName = this.getCurrentUsername(request);
        Long userId = 123456789L;
        String userName = "广电测试";

        return this.couponsService.auditCoupon(couponsId, userId, userName);
    }


    @ApiOperation(value = "优惠券批量审核")
    @PostMapping("/coupons/auditList")
    public Response auditListCoupon(HttpServletRequest request, @ApiParam("优惠券批次id集合") @RequestBody List<Long> couponsIds) {

        //获取用户信息
//        Long userId = this.getCurrentUserId(request);
//        String userName =this.getCurrentUsername(request);
        Long userId = 98765431L;
        String userName = "广电测试";

        return this.couponsService.auditListCoupon(couponsIds, userId, userName);
    }

    @ApiOperation(value = "复制优惠券")
    @ApiImplicitParam(name = "couponsId", value = "优惠券批次id", required = true, dataType = "Long")
    @GetMapping("/coupons/copy/{couponsId}")
    public Response copyCoupons(HttpServletRequest request, @ApiParam("优惠券id") @PathVariable("couponsId") Long couponsId) {

        //获取用户信息
//        Long userId = this.getCurrentUserId(request);
//        String userName = this.getCurrentUsername(request);
        Long userId = 123456789L;
        String userName = "广电测试";

        return Response.success(this.couponsService.copyCoupons(couponsId, userId, userName));
    }


    @ApiOperation(value = "查询优惠券操作人日志")
    @ApiImplicitParam(name = "couponsId", value = "优惠券批次id", required = true, dataType = "Long")
    @GetMapping("/OperationLog/query/{couponsId}")
    public Response queryOperationLog(@ApiParam("优惠券id") @PathVariable("couponsId") Long couponsId) {

        return Response.success(this.couponsService.queryOperationLog(couponsId));
    }

    @ApiOperation(value = "运营管理人员审核优惠券")
    @ApiImplicitParam(name = "couponsId", value = "运营管理人员审核优惠券", required = true, dataType = "Long")
    @PostMapping("/operate/audit/{couponsId}")
    public Response auditCoupons(HttpServletRequest request, @ApiParam("优惠券id") @PathVariable("couponsId") Long couponsId,
                                 @ApiParam(value = "审核参数") @RequestBody CouponsAuditDto dto) {

        //获取用户信息
//        Long userId = this.getCurrentUserId(request);
//        String userName = this.getCurrentUsername(request);
        Long userId = 123456789L;
        String userName = "广电测试";


        return this.couponsService.auditCoupons(couponsId, dto, userId, userName);

    }


    @ApiOperation(value = "运营管理人员批量审核优惠券")
    @PostMapping("/operate/auditList")
    public Response auditListCoupons(HttpServletRequest request, @ApiParam(value = "审核参数") @RequestBody CouponsAuditListDto dto) {

        //获取用户信息
//        Long userId = this.getCurrentUserId(request);
//        String userName = this.getCurrentUsername(request);
        Long userId = 123456789L;
        String userName = "广电测试";

        return this.couponsService.auditListCoupons(dto, userId, userName);

    }


    @ApiOperation(value = "财务管理人员审核优惠券")
    @ApiImplicitParam(name = "couponsId", value = "财务管理人员审核优惠券", required = true, dataType = "Long")
    @PostMapping("/finance/audit/{couponsId}")
    public Response auditFinanceCoupons(HttpServletRequest request, @ApiParam("优惠券id") @PathVariable("couponsId") Long couponsId,
                                        @ApiParam(value = "审核参数") @RequestBody CouponsAuditDto dto) {

        //获取用户信息
//        Long userId = this.getCurrentUserId(request);
//        String userName = this.getCurrentUsername(request);
        Long userId = 123456789L;
        String userName = "广电测试";
        return this.couponsService.auditFinanceCoupons(couponsId, dto, userId, userName);

    }


    @ApiOperation(value = "财务管理人员批量审核优惠券")
    @PostMapping("/finance/auditList")
    public Response auditFinanceListCoupons(HttpServletRequest request,
                                            @ApiParam(value = "审核参数") @RequestBody CouponsAuditListDto dto) {

        //获取用户信息
//        Long userId = this.getCurrentUserId(request);
//        String userName = this.getCurrentUsername(request);
        Long userId = 123456789L;
        String userName = "广电测试";

        return this.couponsService.auditFinanceListCoupons(dto, userId, userName);

    }

    /**查询商家信息*/
    @ApiOperation(value = "查询商家信息")
    @GetMapping("/merchant")
    public List<MerchantInformationVo> queryMerchant(){


        return this.couponsService.queryMerchant();
    }



}
